#!/bin/bash
# *************************************
# 功能: 控制节点执行harbor环境部署
# 作者: 王树森
# 联系: wangshusen@sswang.com
# 版本: v0.1
# 日期: 2023-07-17
# *************************************
# 问题：混合功能的脚本编写
# - 涉及到软件环境的部署 - 单独一个脚本 - 远程主机执行
# - 涉及到软件服务的操作 - 单独一个脚本 - 在管理节点上执行
# 步骤：
# - 先把所有功能放到一个脚本中(到远程主机上执行)
# - 根据功能属性的特质，进行脚本的拆分。

# harbor文件获取
get_harbor(){
  # 获取harbor软件文件
  if [ ! -f "${softs_dir}/${harbor_softs}" ]; then
    cd "${softs_dir}"
    wget "${harbor_url}"
  fi
  remote_status=$(ssh "${login_user}@${harbor_addr}" "[ -f ${target_dir}/${harbor_softs} ] && echo exists")
  if [ "${remote_status}" == "exists" ]; then
    echo -e "\e[33m远程主机已存在 ${harbor_softs} 文件!!!\e[0m"
  else
    ssh "${login_user}@${harbor_addr}" "mkdir -p ${target_dir}"
    scp "${softs_dir}/${harbor_softs}" "${login_user}@${harbor_addr}:${target_dir}/" 
  fi
}

# harbor用户定制
harbor_user(){
  # 接收环境变量
  user="$1"
  passwd="$2"
  # 定制harbor管理员用户配置
  cat > /tmp/add_user.json<<-eof
{
  "username": "${user}",
  "realname": "${user}",
  "email": "${user}@sswang.com",
  "password": "${passwd}",
  "comment": "普通用户"
}
eof
  # 检查用户
  result=$(curl -s -H "Content-Type: application/json" -u "${harbor_admin}:${harbor_admin_passwd}" "${harbor_url}/${add_user_api}" | grep "${user}")
  if [ "${result}" != "" ]; then
    echo -e "\e[32m${user}用户已创建成功!!!\e[0m"
  else
    # 创建用户
    curl -s -H "Content-Type: application/json" -u "${harbor_admin}:${harbor_admin_passwd}" -X POST "${harbor_url}/${add_user_api}" -d @/tmp/add_user.json
    echo -e "\e[31m${user}用户创建成功!!!\e[0m"
    # exit
  fi
}

# harbor镜像仓库
harbor_proj(){
  # 接收环境变量
  user="$1"
  passwd="$2"
  repo="$3"
  # 定制harbor镜像仓库配置
  cat > /tmp/add_repo.json <<-eof
{
  "project_name": "${repo}",
  "public": true
}
eof
  
  # 检测harbor镜像仓库
  result=$(curl -s -H "Content-Type: application/json" -u "${user}:${passwd}" "${harbor_url}/${add_proj_api}" | grep "${repo}")
  if [ "${result}" != "" ]; then
    echo -e "\e[32m${repo}仓库已创建成功!!!\e[0m"
  else
    # 创建harbor镜像仓库
    curl -s -H "Content-Type: application/json" -u "${user}:${passwd}" -X POST "${harbor_url}/${add_proj_api}" -d @/tmp/add_repo.json
    echo -e "\e[31m${repo}仓库创建成功!!!\e[0m"
    # exit
  fi
}

# harbor环境检查
harbor_check(){
  # 检查harbor的应用环境
  echo -e "\e[31m检测harbor程序状态是否正常\e[0m"
  status=$(ssh ${login_user}@${harbor_addr} "systemctl is-active harbor")
  if [ "${status}" == "active" ]; then
    echo -e "\e[32mharbor镜像仓库环境部署成功!!!\e[0m"
  else
    echo -e "\e[31mharbor镜像仓库环境部署失败!!!\e[0m"
    exit
  fi
  # 检查harbor的api服务环境
  echo -e "\e[31m检测harbor服务状态是否正常\e[0m"
  msg=$(curl -s "${harbor_url}/${harbor_ver}")
  ver=$(echo "${msg}" | jq -r ".version")
  mark=''
  while [ "${ver}" != "v2.0" ]; do
    printf "progress: [%-40s]\r" "${mark}"
    sleep 0.2
    mark="#${mark}"
    msg=$(curl -s "${harbor_url}/${harbor_ver}")
    ver=$(echo "${msg}" | jq -r ".version")
  done
  echo -e "\e[32m检测harbor API服务状态正常\e[0m"
}

# 主函数
main(){
  # 控制节点获取harbor文件
  get_harbor
  # 指挥远程harbor主机部署harbor
  remote_harbor_status=$(ssh "${login_user}@${harbor_addr}" "systemctl is-active ${harbor_name}")
  if [ "${remote_harbor_status}" == "active" ]; then
    echo -e "\e[32m${harbor_addr} 主机harbor服务已部署成功\e[0m"
  else
    if [ -f "${scripts_dir}/${base_harbor_remote_scripts}" ]; then
      scp "${scripts_dir}/${base_harbor_remote_scripts}" "${login_user}@${harbor_addr}:/tmp/"
      # 优化1：harbor服务部署动作幂等性的判断
      # ssh "${login_user}@${harbor_addr}" "systemctl stop harbor.service" 2>/dev/null
      ssh "${login_user}@${harbor_addr}" "/bin/bash /tmp/${base_harbor_remote_scripts}"
    fi
  fi
  # 远程检测harbor主机服务是否正常
  harbor_check
  # harbor仓库服务定制
  harbor_user "${harbor_user}" "${harbor_passwd}"
  harbor_proj "${harbor_user}" "${harbor_passwd}" "${harbor_my_repo}"  
  harbor_proj "${harbor_user}" "${harbor_passwd}" "${harbor_k8s_repo}"  
}

# 执行主函数
main
